package logic

import (
	"context"
	"storage/models"

	"storage/internal/svc"
	"storage/pb/storage"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateFileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateFileLogic {
	return &UpdateFileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UpdateFile 更新文件
func (l *UpdateFileLogic) UpdateFile(in *storage.FileInfo) (*storage.Empty, error) {
	file := &models.Files{Id: in.Id}
	if in.Status != 0 {
		file.Status = in.Status
	}
	if in.ParentId >= 0 {
		file.ParentId = in.ParentId
	}
	if in.FileName != "" {
		file.FileName = in.FileName
	}
	if in.FilePath != "" {
		file.FilePath = in.FilePath
	}
	if in.FileType > 0 {
		file.FileType = in.FileType
	}
	if in.FileExt != "" {
		file.FileExt = in.FileExt
	}
	if in.FileSize != 0 {
		file.FileSize = in.FileSize
	}
	if in.FileHash != "" {
		file.FileHash = in.FileHash
	}
	err := l.svcCtx.FilesModel.Update(l.ctx, file)
	if err != nil {
		return nil, err
	}
	return &storage.Empty{}, nil
}
